<template>
  <div class="mask" v-if="showCart" @click="handleShowCart"></div>
  <div class="panelCart">
    <div class="top" v-if="showCart">
      <div
        class="top__icon iconfont"
        v-html="result.allCheck ? '&#xe64c;' : '&#xe834;'"
        @click="
          () => {
            selectAll(shopId);
          }
        "
      ></div>
      <div class="top__txt first">全选</div>
      <div class="top__txt" @click="() => clearCart(shopId)">清空购物车</div>
    </div>
    <div class="right" v-if="showCart">
      <template v-for="(item, key) in dataItemList" :key="key">
        <div class="right__item" v-if="item.count > 0">
          <div
            class="right__item__icon iconfont"
            v-html="item.check ? '&#xe64c;' : '&#xe834;'"
            @click="
              () => {
                handleCheck(shopId, item._id);
              }
            "
          ></div>
          <div class="item__img">
            <img class="img" :src="item.imgUrl" alt="" />
          </div>
          <div class="item__txt">
            <p class="item__title">{{ item.name }}</p>
            <p class="item__price">
              <span class="normalPrice">&yen;{{ item.price }} </span>
              <span class="discount">&yen;{{ item.oldPrice }}</span>
            </p>
          </div>
          <div class="btn">
            <BtnNum
              :count="item.count"
              @plus="
                () => {
                  addToCart(shopId, item._id, item);
                }
              "
              @minus="
                () => {
                  minusToCart(shopId, item._id, item);
                }
              "
            />
          </div>
        </div>
      </template>
    </div>
    <div class="bottom">
      <div class="cart">
        <span class="iconfont" @click="handleShowCart">&#xe600;</span>
        <span class="cart__num">{{ result.totalCount }}</span>
      </div>
      <div class="price">
        <span>总计:</span>
        <span class="price__num">&yen;{{ result.totalPrice }}</span>
      </div>
      <div class="btn" v-show="result.totalCount > 0">
        <router-link
          class="btn__a"
          :to="{ path: `/orderconfirmation/${shopId}` }"
        >
          结算
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref, watchEffect } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
import BtnNum from "./BtnNum";
import { useCommonCartEffect } from "@/effects/cartEffect.js";
export default {
  name: "cart",
  components: { BtnNum },
  setup() {
    const route = useRoute();
    const store = useStore();
    const shopId = route.params.id;
    const cartList = store.state.cartList;

    const dataItemList = computed(() => {
      const list = cartList[shopId]?.productList || [];
      return list;
    });
    //计算属性

    //加减购物车按钮相关
    const { addToCart, minusToCart, result } = useCommonCartEffect(shopId);
    // 选取购物车内容
    const handleCheck = (shopId, id) => {
      store.commit("handleCheckStore", { shopId, id });
    };
    const clearCart = (shopId) => {
      store.commit("clearCartStore", shopId);
    };
    const selectAll = (shopId) => {
      store.commit("selectAllStore", shopId);
    };

    //是否展示蒙层加小购物车
    let showCart = ref(false);
    const handleShowCart = () => {
      showCart.value = !showCart.value;
    };
    watchEffect(() => {
      if (result.value.totalCount === 0) {
        showCart.value = false;
      }
    });

    return {
      result,
      dataItemList,
      shopId,
      addToCart,
      minusToCart,
      handleCheck,
      clearCart,

      selectAll,
      showCart,
      handleShowCart,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0.5rem;
  background-color: rgba($color: #000000, $alpha: 0.5);
}

.panelCart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  box-sizing: border-box;
}

.top {
  width: 100vw;
  height: 0.5rem;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.12rem 0 0.12rem;
  &__icon {
    color: $btn-color;
    font-size: 0.18rem;
  }
  &__txt {
    font-size: 0.14rem;
    color: $content-color;
    line-height: 0.16rem;
  }
  .first {
    margin-right: 1.9rem;
  }
}

.right {
  overflow-y: scroll;
  width: 100vw;
  background: #fff;
  margin-bottom: 0.5rem;
  &__item {
    height: 0.7rem;
    position: relative;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: solid 1px $bgc-color;
    &__icon {
      margin-left: 0.12rem;
      color: $btn-color;
      font-size: 0.18rem;
    }
    .item__img {
      margin-left: 0.18rem;
      display: flex;
      justify-content: center;
      align-items: center;
      .img {
        height: 0.46rem;
        width: 0.46rem;
      }
    }
    .item__txt {
      margin-top: 0.15rem;
      height: 0.69rem;
      width: 50%;
      .item__title {
        line-height: 0.3rem;
        font-size: 0.14rem;
        color: $content-color;
      }
      .item__price {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        line-height: 0.25rem;
        color: $highlight-color;
        .discount {
          text-decoration: line-through;
          color: $grey-color;
          font-size: 0.1rem;
        }
      }
    }
    .btn {
      margin-right: 0.18rem;
    }
  }
}

.bottom {
  width: 100%;
  height: 0.5rem;
  position: absolute;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .cart {
    position: relative;
    width: 0.8rem;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: $btn-color;
    .iconfont {
      font-size: 0.3rem;
    }
    &__num {
      display: block;
      position: absolute;
      right: 0.2rem;
      top: 0.05rem;
      width: 0.15rem;
      height: 0.15rem;
      border-radius: 50%;
      background-color: $highlight-color;
      line-height: 0.15rem;
      text-align: center;
      color: #fff;
      font-size: 0.1rem;
    }
  }
  .price {
    height: 0.5rem;
    line-height: 0.5rem;
    margin-right: 0.8rem;
    font-size: 0.12rem;
    color: $content-color;
    .price__num {
      margin-left: 0.08rem;
      color: $highlight-color;
      font-size: 0.14rem;
    }
  }
  .btn {
    width: 1rem;
    height: 0.5rem;
    line-height: 0.5rem;
    text-align: center;
    font-size: 0.16rem;
    background-color: $btn-color;
    color: #fff;
    a {
      color: #fff !important;
      text-decoration: none;
    }
  }
}
</style>
